package renguanyu.app.auth4fx.view.user;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import renguanyu.app.auth4fx.App;
import renguanyu.app.auth4fx.controller.user.UserInfoUpdateController;
import renguanyu.app.auth4fx.model.user.UserVo;

public class UserInfoUpdateView extends BorderPane {

    private UserListView userListView;
    public UserInfoUpdateView(UserListView userListView){
        this.userListView = userListView;
        VBox formPane = createFormPane(); // 表单面板
        HBox bottomBar = createOperatePane(); // 操作面板
        super.setBottom(bottomBar); // 组装
        super.setCenter(formPane);
        initData();
    }

    public StringProperty nameProperty = new SimpleStringProperty();
    public StringProperty passwordProperty = new SimpleStringProperty();
    private VBox createFormPane() {
        Label nameLabel = new Label("用户名：");
        Label passwordLabel = new Label("密码：");
        TextField nameField = new TextField();
        TextField passwordField = new TextField();
        HBox nameRow = new HBox(nameLabel, nameField);
        HBox passwordRow = new HBox(passwordLabel, passwordField);
        VBox formPane = new VBox(nameRow, passwordRow);

        nameLabel.setAlignment(Pos.CENTER_RIGHT);
        nameLabel.setMinWidth(60);

        nameField.textProperty().bindBidirectional(nameProperty);
        HBox.setHgrow(nameField, Priority.ALWAYS);

        passwordLabel.setAlignment(Pos.CENTER_RIGHT);
        passwordLabel.setMinWidth(60);

        passwordField.textProperty().bindBidirectional(passwordProperty);
        HBox.setHgrow(passwordField,Priority.ALWAYS);

        nameRow.setAlignment(Pos.CENTER_LEFT);
        VBox.setMargin(nameRow, new Insets(0, 0, 10, 0));
        passwordRow.setAlignment(Pos.CENTER_LEFT);
        VBox.setMargin(passwordRow, new Insets(0, 0, 10, 0));
        formPane.setPadding(new Insets(15, 15, 15, 15));
        return formPane;
    }

    private HBox createOperatePane() {
        Button updateBtn = new Button("修改");
        Button backBtn = new Button("返回");
        HBox bottomBar = new HBox(10, updateBtn, backBtn);

        updateBtn.setOnAction(e-> new UserInfoUpdateController(this).startAsyncUpdate());
        backBtn.setOnAction(e-> App.mainView.setCenter(new UserListView()));
        bottomBar.setAlignment(Pos.CENTER);
        bottomBar.setPadding(new Insets(15, 15, 15, 15));
        return bottomBar;
    }

    public UserVo selectedUser;
    private void initData() {
        selectedUser = userListView.selectedUser;
        nameProperty.set(selectedUser.getName());
        passwordProperty.set(selectedUser.getPassword());
    }

}
